package cn.hit.tianqiao.web.servlet;

import cn.hit.tianqiao.domain.Event;
import cn.hit.tianqiao.service.EventService;
import cn.hit.tianqiao.service.impl.EventServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/event/*")
public class EventServlet extends HttpServlet {

    EventService service = new EventServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Event> EventList = service.findEvents();

        // 将结果转换为 JSON 格式并返回
        ObjectMapper mapper = new ObjectMapper();
        String jsonEventList = mapper.writeValueAsString(EventList);
        resp.setContentType("application/json");
        resp.getWriter().write(jsonEventList);
    }
}